use axum::{
    Router,
    routing::get,
    response::Html,
    response::Redirect,
};
use sqlx::mysql::MySqlPoolOptions;
use std::env;
use dotenv::dotenv;
use crate::routes::{table_routes, item_routes};

mod routes;
mod state;
mod handlers;
mod models;

#[tokio::main]
async fn main() {
    dotenv().ok();
    let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");

    let pool = MySqlPoolOptions::new()
        .connect(&database_url)
        .await
        .expect("Failed to create pool.");

    let app = Router::new()
        .merge(table_routes::routes(pool.clone()))
        .merge(item_routes::routes(pool.clone()))
        .route("/", get(|| async { Redirect::to("/forum") }))
        .route("/docs", get(docs_handler))
        .route("/gui", get(gui_handler))
        .route("/forum", get(forum_handler));

    axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
        .serve(app.into_make_service())
        .await
        .unwrap();
}

async fn docs_handler() -> Html<String> {
    Html(include_str!("../static/swagger-ui.html").to_string())
}

async fn gui_handler() -> Html<String> {
    Html(include_str!("../static/GUI.html").to_string())
}

async fn forum_handler() -> Html<String> {
    Html(include_str!("../static/Forum.html").to_string())
}